1   /************************************************************
2   *                     Copyright                            *
3   * Portions of this software are Copyright (c) 1993 - 2002, *
4   * Chad Z. Hower (Kudzu) and the Indy Pit Crew              *
5   *  - http://www.nevrona.com/Indy/                          *
6   ************************************************************/
7   package org.indy;
8   
9   import java.io.IOException;
10  import java.io.InterruptedIOException;
11  
12  import java.net.ServerSocket;
13  
14  import org.indy.io.IOHandler;
15  import org.indy.io.IndyIOException;
16  
17  import org.indy.util.IndyUtilities;
18  
19  
20  /***
21   * DOCUMENT ME!
22   *
23   * @version $Revision$
24   * @author $author$
25   */
26  public final class ListenerThread extends IndyThread {
27    /***
28     *
29     */
30    private final TCPServer server;
31    private final ServerSocket binding;
32  
33    /***
34     *  Constructor for the IdListenerThread object
35     *
36     *@param  server   Description of the Parameter
37     *@param  binding  Description of the Parameter
38     */
39    public ListenerThread(TCPServer server, ServerSocket binding) {
40      super();
41  
42      if ((server == null) || (binding == null)) {
43        throw new NullPointerException(IndyUtilities.getResourceString(
44                                           "RSNullBinding"));
45      }
46  
47      this.server = server;
48      this.binding = binding;
49    }
50  
51    /***
52     *  Gets the server attribute of the IdListenerThread object
53     *
54     *@return    The server value
55     */
56    public TCPServer getServer() {
57      return server;
58    }
59  
60    /***
61     *  Main processing method for the IdListenerThread object
62     */
63    public void run() {
64      try {
65        while (!isStopped()) {
66          IOHandler ioHandler = null;
67  
68  
69          //set a low timeout so that interupptions can be caught (allowing the thread to be terminated :)
70          binding.setSoTimeout(1000);
71  
72          try {
73            ioHandler = server.getIOHandler().accept(binding, this);
74          }
75           catch (InterruptedIOException ioe) {
76            if (!isStopped()) {
77              continue;
78            }
79          }
80  
81          if (ioHandler == null) {
82            stop();
83  
84            return;
85          }
86          else {
87            TCPServerConnection peer = new TCPServerConnection(server, ioHandler);
88  
89            PeerThread thread = server.getThreadManager().getThread();
90  
91            thread.setConnection(peer);
92  
93            if ((server.getMaxConnections() > 0) && 
94                  (server.getThreadList().size() > server.getMaxConnections())) {
95              server.getThreadManager().releaseThread(thread);
96              peer.writeRFCReply(server.getMaxConnectionReply());
97              peer.disconnect();
98              peer = null;
99            }
100           else {
101             server.getThreadList().add(thread);
102             thread.start();
103           }
104         }
105       }
106     }
107      catch (Exception e) {
108       e.printStackTrace();
109       server.doListenException(this, new IndyException(e));
110     }
111   }
112 
113   /***
114    *  Description of the Method
115    */
116   protected synchronized void afterExecute() throws InterruptedException {
117     super.afterRun();
118 
119     try {
120       binding.close();
121     }
122      catch (IOException ioe) {
123       ioe.printStackTrace();
124       server.doListenException(this, new IndyIOException(ioe));
125     }
126   }
127 }
This page was automatically generated by Maven